<?php
class Comment {
	private static function update($table, $post_data)
	{		
		if (isset($post_data[Protocol::ID])) {
			// On Update
			$data[Protocol::ID] 			= $post_data[Protocol::ID];
			$data[Protocol::LIKES]			= $post_data[Protocol::LIKES];
		}
		else {
			// On Create
			$data[Protocol::USER_ID]		= $post_data[Protocol::USER_ID];
			$data[Protocol::CREATED_TIME]	= time();
		}

		$data[Protocol::COMMENT]			= $post_data[Protocol::COMMENT];
		
		if ($table == Protocol::MODEL_COMMENT)
			$data[Protocol::MODEL_ID]		= $post_data[Protocol::TARGET_ID];
		else if ($table == Protocol::PHOTO_COMMENT)
			$data[Protocol::USER_ID]		= $post_data[Protocol::TARGET_ID];
		
		return DBConnector::update($table, $data);
	}
	
	public static function update_model_comment($post_data)
	{
		self::update(Protocol::MODEL_COMMENT, $post_data);
	}
	
	public static function update_photo_comment($post_data)
	{
		self::update(Protocol::PHOTO_COMMENT, $post_data);
	}
	
	private static function get_list(
	$table,	$start = FALSE, $end = FALSE, $keyword = FALSE, $order = FALSE, $where = FALSE)
	{
		$fields = array(
			Protocol::NAME." AS ".Protocol::USER_NAME,
			$table.".".Protocol::ID, $table.".".Protocol::CREATED_TIME, $table.".".Protocol::LIKES,
			Protocol::USER_ID, Protocol::COMMENT
		);
		
		if ($table == Protocol::MODEL_COMMENT)
			array_push($fields, Protocol::MODEL_ID);
		else if ($table == Protocol::PHOTO_COMMENT)
			array_push($fields, Protocol::PHOTO_ID);
		else ;
		
		$join = "INNER JOIN ".Protocol::USER." ON ".Protocol::USER_ID." = ".Protocol::USER.".".Protocol::ID;
		$search_fields = array(Protocol::NAME, Protocol::COMMENT);
		return DBConnector::get_list($table, $start, $end, $search_fields, $keyword, $order, $fields, $join);
	}
	
	public static function get_model_comment_list(
	$model_id = FALSE, $start = FALSE, $end = FALSE, $keyword = FALSE, $order = FALSE)
	{
		$where = FALSE;
		if ($model_id !== FALSE)
			$where = Protocol::MODEL_ID." = $model_id";
		return self::get_list(Protocol::MODEL_COMMENT, $start, $end, $keyword, $order, $where);
	}
	
	public static function get_photo_comment_list(
	$photo_id = FALSE, $start = FALSE, $end = FALSE, $keyword = FALSE, $order = FALSE)
	{
		$where = FALSE;
		if ($photo_id !== FALSE)
			$where = Protocol::PHOTO_ID." = $photo_id";
		return self::get_list(Protocol::PHOTO_COMMENT, $start, $end, $keyword, $order, $where);
	}
	
	private static function get($table, $value, $key = FALSE)
	{
		if ($key === FALSE) $key = Protocol::ID;
		return DBConnector::get($table, $key, $value);
	}
	
	public static function get_model_comment($value, $key = FALSE)
	{
		return self::get(Protocol::MODEL_COMMENT, $value, $key);
	}
	
	public static function get_photo_comment($value, $key = FALSE)
	{
		return self::get(Protocol::PHOTO_COMMENT, $value, $key);
	}
	
	private static function delete($table, $id_list)
	{
		return DBConnector::delete($table, $id_list);
	}
	
	public static function delete_model_comment($id_list)
	{
		self::delete(Protocol::MODEL_COMMENT, $id_list);
	}
	
	public static function delete_photo_comment($id_list)
	{
		self::delete(Protocol::PHOTO_COMMENT, $id_list);
	}
}
